import os
from src.mod import run
from src.core import log
from src.core.variables.constant import CONF
from src.core.variables.status import SUCCESS_CODE_CUSTOMIZE


def fm_occ_space():
    def get_dir_size(start_path="."):
        """递归计算目录总大小（最快方式）"""
        total_size = 0
        with os.scandir(start_path) as it:
            for entry in it:
                if entry.is_file():
                    total_size += entry.stat().st_size
                elif entry.is_dir():
                    total_size += get_dir_size(entry.path)
        return total_size
    def convert_size(size_bytes):
        if size_bytes == 0:
            return "0B"
        units = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
        i = 0
        while size_bytes >= 1024 and i < len(units) - 1:
            size_bytes /= 1024
            i += 1
        return f"{size_bytes:.2f} {units[i]}"
    total_size = get_dir_size(CONF.PATH.ABS)
    return convert_size(total_size)